/**
 * Copyright 2020 jianggujin (www.jianggujin.com).
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jianggujin.modulelink.support.inject;

import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.jianggujin.modulelink.action.JActionWrapper;
import com.jianggujin.modulelink.exception.JInjectException;
import com.jianggujin.modulelink.module.JModule;
import com.jianggujin.modulelink.support.JBeanProvider;
import com.jianggujin.modulelink.util.JBeanUtils;

/**
 * 注入指定类型Bean集合
 * 
 * @author jianggujin
 *
 */
public class JInjectCollection extends JAbstractInject {

    public JInjectCollection(Object target, Field field, com.jianggujin.modulelink.support.annotation.JInject inject) {
        super(target, field, inject);
    }

    @SuppressWarnings("unchecked")
    @Override
    protected Object handleValue(JBeanProvider provider, JModule<?> module) throws Exception {
        Type[] componentTypes = JBeanUtils.getComponentType(field);
        if (componentTypes == null || componentTypes.length == 0) {
            componentTypes = new Type[] { Object.class };
        }
        if (!(componentTypes[0] instanceof Class<?>)) {
            throw new JInjectException("required [" + field.getName() + "] value type invalid");
        }
        Class<?> componentType = (Class<?>) componentTypes[0];
        int size = 0;
        Map<String, ?> values = provider.optBeansOfType(componentType);
        size += (values == null ? 0 : values.size());
        Collection<?> actions = null;
        if (JActionWrapper.class.equals(componentType)) {
            actions = module.getActionWrappers();
        } else {
            List<Object> filters = new ArrayList<Object>();
            for (JActionWrapper wrapper : module.getActionWrappers()) {
                if (componentType.isAssignableFrom(wrapper.getActionClass())) {
                    filters.add(wrapper.getAction());
                }
            }
            actions = filters;
        }
        size += (actions == null ? 0 : actions.size());
        if (size == 0) {
            return null;
        }

        Class<?> fieldType = field.getType();
        Collection<Object> value = null;
        if (fieldType.isInterface()) {
            if (List.class.isAssignableFrom(fieldType) || Collection.class.equals(fieldType)) {
                value = new ArrayList<Object>();
            } else if (Set.class.isAssignableFrom(fieldType)) {
                value = new HashSet<Object>();
            }
        } else {
            value = (Collection<Object>) fieldType.newInstance();
        }

        if (values != null) {
            value.addAll(values.values());
        }
        if (actions != null) {
            value.addAll(actions);
        }
        return value;
    }
}
